/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package jsf.bean.client;

import JDBCUtil.Constants;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import model.client.City;
import model.client.Country;

/**
 *
 * @author Administrator
 */
@ManagedBean
@SessionScoped
public class RegisterBean_c implements Serializable {
    private final Connection conn;
    //Field name constant
    private final String CITY_ID = "City_Id";
    private final String CITY_NAME = "City_Name";
    private final String COUNTRY_ID = "Country_Id";
    private final String COUNTRY_STATUS = "Country_Status";
    private final String COUNTRY_NAME = "Country_Name";
    //Properties
    private String username;
    private String password;
    private String email;
    private int paymentTypeId = 4;
    private String fullName;
    private Date dateOfBirth;
    private String gender;
    private String contactNumber;
    private int casteId;
    private String maritalStatus;
    private int religionId;
    private String motherTongue;
    private String cardNumber;
    private String securityCode;
    private boolean agreementChecked = false;
    private Country country;
    private City city;
    private List<City> listCities;
    private List<Country> listCountries;

    public Country getCountry() {
        return country;
    }

    public void setCountry(Country country) {
        this.country = country;
    }

    public City getCity() {
        return city;
    }

    public void setCity(City city) {
        this.city = city;
    }

    public List<City> getListCities() {
        return listCities;
    }

    public List<Country> getListCountries() {
        return listCountries;
    }

    public String getMotherTongue() {
        return motherTongue;
    }

    public void setMotherTongue(String motherTongue) {
        this.motherTongue = motherTongue;
    }

    public String getMaritalStatus() {
        return maritalStatus;
    }

    public void setMaritalStatus(String maritalStatus) {
        this.maritalStatus = maritalStatus;
    }

    public int getReligionId() {
        return religionId;
    }

    public void setReligionId(int religionId) {
        this.religionId = religionId;
    }

    
    public String getCardNumber() {
        return cardNumber;
    }

    public void setCardNumber(String cardNumber) {
        this.cardNumber = cardNumber;
    }

    public String getSecurityCode() {
        return securityCode;
    }

    public void setSecurityCode(String securityCode) {
        this.securityCode = securityCode;
    }
            

    public boolean isAgreementChecked() {
        return agreementChecked;
    }

    public void setAgreementChecked(boolean agreementChecked) {
        this.agreementChecked = agreementChecked;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public int getPaymentTypeId() {
        return paymentTypeId;
    }

    public void setPaymentTypeId(int paymentTypeId) {
        this.paymentTypeId = paymentTypeId;
    }

    public String getContactNumber() {
        return contactNumber;
    }

    public void setContactNumber(String contactNumber) {
        this.contactNumber = contactNumber;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public Date getDateOfBirth() {
        return dateOfBirth;
    }

    public void setDateOfBirth(Date dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public int getCasteId() {
        return casteId;
    }

    public void setCasteId(int casteId) {
        this.casteId = casteId;
    }
    
    /**
     * Creates a new instance of CountryBean
     *
     * @throws java.sql.SQLException
     * @throws java.lang.ClassNotFoundException
     */
    public RegisterBean_c() throws SQLException, ClassNotFoundException {
        conn = JDBCUtil.MyConn.getConnection();
    }
    
    @PostConstruct
    public void listAllCountries() throws SQLException {
        PreparedStatement pst = conn.prepareStatement("SELECT * FROM " + Constants.TABLE_COUNTRIES);
        ResultSet rs = pst.executeQuery();
        
        listCountries = new ArrayList<Country>();
        while (rs.next()) {
            listCountries.add(new Country(rs.getInt(COUNTRY_ID), rs.getBoolean(COUNTRY_STATUS), rs.getString(COUNTRY_NAME)));
        }
    }

    public void listAllCitiesByCountryId() throws SQLException {
        PreparedStatement pst = conn.prepareStatement("SELECT * FROM " + Constants.TABLE_CITIES
                + " WHERE " + COUNTRY_ID + " = " + country.getCountryId());
        ResultSet rs = pst.executeQuery();
        listCities = new ArrayList<City>();
        while (rs.next()) {
            listCities.add(new City(rs.getInt(CITY_ID), rs.getInt(COUNTRY_ID), rs.getString(CITY_NAME)));
        }
    }
    
    public void registerAcc() throws SQLException {
        PreparedStatement pst = conn.prepareStatement("INSERT INTO " + Constants.TABLE_ACCOUNTS + " VALUES(?,?,?,?,?)");
        pst.setString(1, password);
        pst.setString(2, username);
        pst.setString(3, email);
        pst.setBoolean(4, true);
        pst.setString(5, "member");
        pst.executeUpdate();
        
        pst = conn.prepareStatement("INSERT INTO " + Constants.TABLE_ACCOUNT_DETAILS + " VALUES(@@IDENTITY,?,?,?,?,?,?,?,?,?,?,?,?)");
        pst.setString(1, fullName);
        pst.setString(2, gender);
        pst.setDate(3, new java.sql.Date(dateOfBirth.getTime()));
        pst.setString(4, maritalStatus);
        pst.setString(5, motherTongue);
        pst.setFloat(6, 0);
        pst.setInt(7, Integer.valueOf(contactNumber));
        pst.setInt(8, casteId);
        pst.setString(9, "");
        pst.setInt(10, city.getCityId());
        pst.setString(11, "");
        pst.setInt(12, religionId);
        pst.executeUpdate();
        
        
    }

}
